package test;

public class JoinInterruptExample {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("Thread 1 started");
            try {
                Thread.sleep(5000); // 线程 1 睡眠 5 秒钟
            } catch (InterruptedException e) {
                System.out.println("Thread 1 is interrupted:"+e);
                return;
            }
            System.out.println("Thread 1 finished");
        });

        Thread t2 = new Thread(() -> {
            System.out.println("Thread 2 started");
            try {
                Thread.sleep(2000); // 线程 2 睡眠 2 秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t1.interrupt(); // 中断线程 1
            System.out.println("Thread 2 finished");
        });

        t1.start();
        t2.start();

        t1.join(); // 主线程等待线程 1 执行完毕
        t2.join(); // 主线程等待线程 2 执行完毕

        System.out.println("Main thread is finished.");
    }
}

